#ifndef LOAD_DATA_H_INCLUDED
#define LOAD_DATA_H_INCLUDED

#include <algorithm>
using namespace std;




void insert_content(string buf,vector<CONTENT> &text);
string normalization(const string buf);//标准化数据,去空格，化为小写
bool descending( const CONTENT &v1, const CONTENT &v2);
bool content_equal(const CONTENT &left, const CONTENT &right);

void List(const char *path, PNODE father);//递归地列出该目录下的节点并挂载到子树下
void readfile(vector<CONTENT> &text,const char *path);//读取文件内容

void List(const char *path, PNODE father) {
    DIR *pDir=opendir(path);
    father->path=path;
    PNODE yongest_son=NULL;
    struct dirent* dirp = NULL;
    string newpath;
    while ((dirp = readdir(pDir)) != NULL) {
        newpath=path;
        newpath+="/";
        newpath+=dirp->d_name;
        if (dirp->d_type == 8) {//文件
            if (strcmp(dirp->d_name, "feature.txt")==0) {
                readfile(father->feature,newpath.c_str());
                }
            else {
                if (yongest_son == NULL) {
                    yongest_son=new NODE;
                    father->son=yongest_son;
                    }
                else {
                    yongest_son->brother=new NODE;
                    yongest_son=yongest_son->brother;
                    }
                yongest_son->father=father;
                string tmp=dirp->d_name;
                size_t pos1,pos2,pos3;//分别为varcode，tag，name
                pos1=tmp.find(".txt");
                if (pos1!=string::npos) tmp=tmp.substr(0,pos1);//删除掉文件名中的.txt
                pos1=tmp.find('_');
                pos2=tmp.find('_',pos1+1);
                pos3=tmp.find('_',pos2+1);
                yongest_son->varcode=tmp.substr(pos1+1,pos2-pos1-1);
                if(pos3-pos2==1) {
                    yongest_son->tag='0';
                    }
                else {
                    yongest_son->tag=tmp.substr(pos2+1,pos3-pos2-1);
                    }
                yongest_son->name=tmp.substr(pos3+1);
                yongest_son->path=newpath;
                readfile(yongest_son->value,newpath.c_str());
                }
            }
        else if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
            if (yongest_son == NULL) {
                yongest_son=new NODE;
                father->son=yongest_son;
                }
            else {
                yongest_son->brother=new NODE;
                yongest_son=yongest_son->brother;
                }
            string tmp=dirp->d_name;
            size_t pos1,pos2,pos3;
            pos1=tmp.find(".txt");
            if (pos1!=string::npos) tmp=tmp.substr(0,pos1);//删除掉文件名中的.txt
            pos1=tmp.find('_');
            pos2=tmp.find('_',pos1+1);
            pos3=tmp.find('_',pos2+1);
            yongest_son->varcode=tmp.substr(pos1+1,pos2-pos1-1);
            if(pos3-pos2==1) {
                    yongest_son->tag='0';
                    }
                else {
                    yongest_son->tag=tmp.substr(pos2+1,pos3-pos2-1);
                    }
            yongest_son->name=tmp.substr(pos3+1);
            yongest_son->father=father;
            yongest_son->path=path;
            List(newpath.c_str(), yongest_son);
            }
        }
    }


void readfile(vector<CONTENT> &text,const char *path) {
    ifstream data(path, ios::in);
    string buf;
    while(data) {
        if(getline(data,buf)) {
            insert_content(buf,text);
            }
        }
    text.resize( unique (text.begin(), text.end(),content_equal) -text.begin() );
    data.close( );
    }

void insert_content(string buf,vector<CONTENT> &text) {
    CONTENT tmp;
    size_t pos;
    if ((pos=buf.find("->"))!=string::npos) {
        tmp.name=normalization(buf.substr(pos+2));
        buf=buf.substr(0,pos);
        }
    while((pos=buf.find("/"))!=string::npos) {
        tmp.nickname=normalization(buf.substr(0,pos));
        if (tmp.name.empty()) tmp.name=tmp.nickname;//没有name时填充上nickname
        if (!tmp.nickname.empty()) {
            text.push_back(tmp);
            }
        buf=buf.substr(pos+1);
        }
    tmp.nickname=normalization(buf);
    if (tmp.name.empty()) tmp.name=tmp.nickname;//没有name时填充上nickname
    if (!tmp.nickname.empty()) {
        text.push_back(tmp);
        }
    }

string normalization(const string buf) {
    char tmp[100];
    unsigned int i,j;
    for(i=0,j=0; i<buf.length(); i++) {
        if (buf[i]==' '&&(j==0||tmp[j-1]==' '))
            continue;
        else
            tmp[j++]=(buf[i]>='A'&&buf[i]<='Z'?buf[i]-'A'+'a':buf[i]);
        }
    if(j!=0&&tmp[j-1]==' ') tmp[j-1]=0;
    else 	tmp[j]=0;
    return tmp;
    }
#endif // LOAD_DATA_H_INCLUDED
